package 链表.回文列表;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/4/19 16:35
 * @description:判断一个链表是否为回文链表 如：1->2->2->1 正反顺序都一样
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        ListNode root = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(1))));
        System.out.println(isPalindrome(root));
    }

    public static boolean isPalindrome(ListNode head) {
       List<Integer> list = new ArrayList<>();
       list.add(head.val);
       ListNode p = head.next;
       if (p == null) {
           return true;
       }
       while (p != null) {
           list.add(p.val);
           p = p.next;
       }
       //集合转数组
        Integer[] array = list.toArray(new Integer[0]);
       int left = 0;
       int right = array.length - 1;
       while (left <= right) {
           if (array[left] != array[right]) {
               return false;
           }
           left++;
           right--;
       }
       return true;
    }
}
